<?php
class Admin_OrderController extends Zend_Controller_Action {
	public function init() {
		/* Initialize action controller here */
	}
	public function indexAction() {
		$orderDb = new Application_Model_DbTable_Order();
		$orders = $orderDb->fetchAll('del_flag = 0');
		$this->view->orders = $orders;
	}
	
	
	public function editAction() {
		$id = (int) $this->getParam('id');
		$orderDb = new Application_Model_DbTable_Order();
		$order = $orderDb->find($id)->current();
		$orderDetails = $order->findDependentRowset('Application_Model_DbTable_OrderDetail');
		
		$this->view->order = $order;
		$this->view->orderDetails = $orderDetails;
		
		if ($this->getRequest()->isPost()) {
			$data = array (
					'phone' 	=> $this->getParam('phone'),
					'address' 	=> $this->getParam('address'),
					'status' 	=> $this->getParam('status')
			);
			
			$whereOrder = $orderDb->getAdapter()->quoteInto('id = ?', $id);
			$orderDb->update($data, $whereOrder);
			$orderDetailDb = new Application_Model_DbTable_OrderDetail();
			
			
			foreach ($orderDetails as $orderDetail) {
				$where = $orderDb->getAdapter()->quoteInto('id = ?', $orderDetail->id);
				$orderDetailDb->update(array('quantity' => $this->getParam('quantity')[$orderDetail->id]), $where );
			}
		}
		$order = $orderDb->find($id)->current();
		$orderDetails = $order->findDependentRowset('Application_Model_DbTable_OrderDetail');
		$this->view->order = $order;
		$this->view->orderDetails = $orderDetails;
	}
	
	public function deleteAction(){
		$id = (int) $this->getParam('id');
		$orderDb = new Application_Model_DbTable_Order();
		$whereOrder = $orderDb->getAdapter()->quoteInto('id = ?', $id);
		$orderDb->update(array('del_flag' => 1), $whereOrder);
		$this->redirect('admin/order');
	}
}

